<?php

namespace App\Admin\Controllers;
use App\Admin\Models\Assets;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;
use App\Admin\Models\Common;
use App\Helps\Functions;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\MessageBag;
use DB;


class AssetsController extends Controller
{
	use HasResourceActions;
	
    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('资产')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('资产')
            ->description('详细')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('资产')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('资产')
            ->description('新增')
            ->body($this->form());
    }
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Assets);
        $grid->saId('ID');
        $grid->spId('资产类型');
        $grid->saName('资产名称');
        $grid->saNum('资产数量');
        $grid->saWorth('资产价值');
        $grid->saOwnStatus('资产状态')->display(function ($OwnStatus){
			switch ($OwnStatus) {
			case 1:
				return "拥有";
				break;
			case 2:
				return "免费借";
				break;
			case 3:
				return "租用";
				break;
			}
		});
        $grid->saRent('每月租金');
        $grid->createTime('上传时间');

		$grid->filter(function($filter){

		    // 去掉默认的id过滤器
		    $filter->disableIdFilter();

		});
		
		$grid->actions(function ($actions) {
//		    $actions->disableDelete();
//		    $actions->disableEdit();
//		    $actions->disableView();
		});

		$grid->filter(function($filter){

		    // 去掉默认的id过滤器
		    $filter->disableIdFilter();
		    
		    $filter->column(1/2, function ($filter) {
		    	
		    	$filter->like('saName', '资产名称');

			});
		});

		// 默认展开筛选框
		$grid->expandFilter();
		
		// 禁用行选择checkbox
		$grid->disableRowSelector();
		
		// 禁用导出
		$grid->disableExport();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
    	$Assets = new Assets();
        $show = new Show($Assets->findOrFail($id));

        $show->saId('ID');
        $show->spId('资产类型');
        $show->saName('资产名称');
        $show->saNum('资产数量');
        $show->saWorth('资产价值');
        $show->saOwnStatus('资产状态')->using(['1' => '拥有', '2' => '免费借', '3' => '租用']);
        $show->saRent('每月租金');
        $show->createTime('上传时间');
		$show->panel()
			->tools(function ($tools) {
//				$tools->disableEdit();
//				$tools->disableList();
//				$tools->disableDelete();
		});;
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::Form(ASSETS::class, function (Form $form) {
			
			$sId = Admin::user()->sId;
			
			$form->hidden('saId');
			$form->select('spId', '资产类型')->options($this->common->getAssetsTypeSelect($sId));
			$form->text('saName', '资产名称');
			$form->number('saNum', '数量')->default(1);
			$form->text('saWorth', '价值');
			$form->select('saOwnStatus', '资产状态')->options([1 => '拥有', 2 => '免费借', '3' => '租用']);
			$form->number('saRent', '每月租金')->default(0);
            $form->tools(function (Form\Tools $tools) {

			    // 去掉`列表`按钮
//			    $tools->disableList();
				
			    // 去掉`删除`按钮
			    $tools->disableDelete();
			
			    // 去掉`查看`按钮
//			    $tools->disableView();
			
			    // 添加一个按钮, 参数可以是字符串, 或者实现了Renderable或Htmlable接口的对象实例
//			    $tools->add('<a class="btn btn-sm btn-danger"><i class="fa fa-trash"></i>&nbsp;&nbsp;delete</a>');
			});
			
			$form->footer(function ($footer) {

//			    // 去掉`重置`按钮
//			    $footer->disableReset();
//			
//			    // 去掉`提交`按钮
//			    $footer->disableSubmit();
			
			    // 去掉`查看`checkbox
			    $footer->disableViewCheck();
			
			    // 去掉`继续编辑`checkbox
			    $footer->disableEditingCheck();
			
			    // 去掉`继续创建`checkbox
//			    $footer->disableCreatingCheck();
			
			});
			
			//保存后回调
			$form->saved(function (Form $form) use($sId) {
			    
			    $sFixedAssets = $this->common->getAccountAssets($sId, 1);
			    $sBorrowAssets = $this->common->getAccountAssets($sId, 2);
			    $sLeaseAssets = $this->common->getAccountAssets($sId, 3);
			    DB::table('store')->where('sId', $sId)->update([
			    	'sFixedAssets' => $sFixedAssets,
			    	'sBorrowAssets' => $sBorrowAssets,
			    	'sLeaseAssets' => $sLeaseAssets,
			    ]);
			});
        });
    }
    
}
